In Svelte, event handlers do not automatically bind this like class methods in traditional JavaScript. Components are not classes by default, so this inside a function does not refer to the component instance. Instead, Svelte encourages using local variables and arrow functions, which capture the correct context.
Here, no this is needed. The increment function directly updates the count variable, and Svelte reactivity handles the DOM updates automatically.
Using an arrow function ensures that the correct variables in the component scope are accessible, without relying on this.
If you define methods inside a JavaScript class and use Svelte with class components, you would need to manually bind this, similar to React class components:
However, using Svelte’s default <script> blocks with let variables is simpler and avoids the need for this entirely.
Svelte components are not classes by default; this is rarely needed.
Use local variables and reactive statements instead of relying on this.
Arrow functions automatically capture component scope variables.
Manual this binding is only necessary if you define class-based components (uncommon in Svelte).